package kr.co.cherryt.activity;

import kr.co.cherryt.cherryt.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

//hs-info// http://stackoverflow.com/questions/1022125/on-android-making-a-phonecall-using-a-browser-application
//hs-info// http://stackoverflow.com/questions/2835556/whats-the-difference-between-setwebviewclient-vs-setwebchromeclient
//hs-TODO// Authenticator로 로그인 상태 유지 방법 고안~ 구현~ (http://cranix.net/347)
public class SettingsSubActivity extends Activity {

	private Handler restartPkgHandler = new Handler();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_sub_activity);

		WebView web = (WebView) this.findViewById(R.id.settingWebView);
		web.getSettings().setSupportMultipleWindows(true);
		web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		web.getSettings().setJavaScriptEnabled(true);
		web.getSettings().setDefaultFontSize(15);
		web.requestFocus(); 
		web.setBackgroundColor(0);
		
		Intent intent=getIntent();
		String preferenceUrl = intent.getExtras().getString("preferenceURL");

		if(preferenceUrl.equals("http://cherryt.co.kr/ct/index.php?mid=ct_ad&m=1&act=dispMemberLogout")){
			web.setWebViewClient(restartPkgClient);
		}
		else if(preferenceUrl.equals("http://cherryt.co.kr/ct/index.php?mid=ct_notice&m=1")
				|| preferenceUrl.equals("http://cherryt.co.kr/ct/index.php?m=1&mid=main_faq")
				|| preferenceUrl.equals("http://cherryt.co.kr/ct/index.php?m=1&mid=myinfo_a")
				|| preferenceUrl.equals("http://cherryt.co.kr/ct/index.php?m=1&mid=myinfo_b")
				|| preferenceUrl.equals("http://cherryt.co.kr/ct/setting/g.html")){
			web.setWebViewClient(new WebViewClient());
			web.setWebChromeClient(new WebChromeClient(){}); 
		}
		web.loadUrl(preferenceUrl);
	}
	
	private WebViewClient restartPkgClient = new WebViewClient(){
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			Toast.makeText(getApplicationContext(), "체리티가 재시작 됩니다.", Toast.LENGTH_SHORT).show();
			restartPkgHandler.postDelayed(new Runnable() {
				public void run() {
					System.exit(0);
				}
			}, 1000);
		}
	};
}